<template>
	<vk-data-dialog
		v-model="value.show"
		:title="page.title"
		:top="page.top"
		:width="page.width"
		:close-on-click-modal="true"
		center
		mode="form"
		@open="onOpen"
		@closed="onClose"
	>
		<view style="padding: 15px 20px;">
			<view v-for="(item,index) in value.list" :key="index">
				<view style="display: flex;align-items: center;">
					<view>
						<vk-data-icon v-if="item.status === 0" name="el-icon-time" size="16" color="#909399"></vk-data-icon>
						<vk-data-icon v-else-if="item.status === 1" name="el-icon-more-outline" size="16" color="#409EFF"></vk-data-icon>
						<vk-data-icon v-else-if="item.status === 2" name="el-icon-circle-check" size="16" color="#67C23A"></vk-data-icon>
						<vk-data-icon v-else-if="item.status === 3" name="el-icon-warning-outline" size="16" color="#E6A23C"></vk-data-icon>
						<vk-data-icon v-else-if="item.status === 4" name="el-icon-circle-close" size="16" color="#F56C6C"></vk-data-icon>
					</view>
					<view style="margin-left: 20px;">
						{{index+1}}、{{ item.no }}
						{{ item.title }}（{{ item.withdrawal_info.name }}）
						提现金额：{{ vk.pubfn.priceFilter(item.withdrawal_money) }} 元
					</view>
				</view>
			</view>
			<view style="margin-top: 20px;" v-if="msg.title">
				当前状态：
				<text v-if="msg.status === 0" style="color: #909399;">{{ msg.title }}</text>
				<text v-if="msg.status === 1" style="color: #409EFF;">{{ msg.title }}</text>
				<text v-if="msg.status === 2" style="color: #67c23a;">{{ msg.title }}</text>
				<text v-if="msg.status === 3" style="color: #F56C6C;">{{ msg.title }}</text>
				<text v-if="msg.status === 4" style="color: #F56C6C;">{{ msg.title }}</text>
			</view>
			<view style="margin-top: 20px;">
				<el-progress :text-inside="true" :stroke-width="20" :percentage="progress.value" status="success"></el-progress>
			</view>
		</view>


		<!-- 页面主体内容开始 -->
		<vk-data-form
			ref="form1"
			v-model="form1.data"
			:form-type="value.mode"
			:rules="form1.props.rules"
			:action="form1.props.action"
			:columns="form1.props.columns"
			:loading.sync="form1.props.loading"
			:labelWidth="form1.props.labelWidth"
			:show-cancel="page.showCancel"
			:cancel-text="page.cancelText"
			:submit-text="page.submitText"
			@success="onFormSuccess"
		>
			<template v-slot:footer>
				<el-button :loading="page.loading" type="danger" size="medium" @click="examine(4)">审核拒绝</el-button>
				<el-button :loading="page.loading" type="primary" size="medium" style="margin-left: 100px;" @click="examine(2)">审核通过</el-button>
			</template>
		</vk-data-form>
		<!-- 页面主体内容结束 -->
	</vk-data-dialog>
</template>

<script>
var that; // 当前页面对象
var vk = uni.vk; // vk实例
export default {
	props: {
		value: {
			Type: Object,
			default: function() {
				return {
					show: false,
					mode: "",
					list: []
				};
			}
		}
	},
	data: function() {
		// 组件创建时,进行数据初始化
		return {
			page: {
				title: "批量审核",
				submitText: "确定",
				cancelText: "关闭",
				showCancel: true,
				top: "7vh",
				width: "900px",
				loading:false
			},
			form1: {
				// 表单请求数据，此处可以设置默认值
				data: {

				},
				// 表单属性
				props: {
					// 表单请求地址
					action: "",
					// 表单字段显示规则
					columns: [
						{
							key:"mode", title:"提现类型", type:"radio",
						  data:[
								{ value: 0, label: "普通提现" },
								{ value: 1, label: "急速提现" },
							],
							tips: "普通提现：手动转账<br/>急速提现：自动转账（不支持转银行卡）"
						},
						{
							key:"seller_remark", title:"商家审核备注", type:"textarea", width:500,
							maxlength:200,
							showWordLimit:true,
							autosize:{ minRows: 5, maxRows: 10 },
						},
					],
					// 表单验证规则
					rules: {
						mode: [
							{ required: true, message: '请选择提现类型', trigger: ['blur','change'] },
						],
					},
					// 左侧label的宽度
					labelWidth: "100px",
				}
			},
			progress:{
				value:0,
				 colors: [
					{color: '#f56c6c', percentage: 20},
					{color: '#e6a23c', percentage: 40},
					{color: '#1989fa', percentage: 60},
					{color: '#1989fa', percentage: 80},
					{color: '#5cb87a', percentage: 100}
				]
			},
			msg:{
				status:0,
				title:""
			}
		};
	},
	mounted() {
		that = this;
		that.init();
	},
	methods: {
		// 初始化
		init() {
			let { value } = that;
			that.$emit("input", value);
		},
		// 监听 - 页面打开
		onOpen() {
			that = this;
			let { list=[] } = that.value;
			// 每次打开时，重新设置表单的值 = value.item 的值，item通过 vk.pubfn.openForm('表单名',{ item:{ _id:"1" } }) 传递值
			let user_ids = vk.pubfn.arrayObjectGetArray(list, "_id");
			that.progress.value = 0;
			that.msg = {
				status:0,
				title:""
			};
			that.form1.data = {
				user_ids
			}
		},
		// 监听 - 页面关闭
		onClose() {
			//that.$refs.form1.resetForm(); // 关闭时，重置表单
		},
		// 监听 - 提交成功后
		onFormSuccess() {
			that.value.show = false; // 关闭页面
			that.$emit("success");
		},
		async examine(status){
			// 写循环
			let { list=[] } = that.value;
			that.msg = {
				status:0,
				title:""
			};
			if(typeof that.form1.data.mode !== "number"){
				that.msg = {
					status:4,
					title:"请选择提现类型"
				};
				vk.toast("请选择提现类型");
				return false;
			}
			let num = 0;
			let sleep = 1000;
			that.progress.value = 0;
			vk.showLoading("执行中...");
			that.page.loading = true;
			for(let i=0; i<list.length; i++){
				sleep = 1000;
				let item = list[i];
				if(item.status === 0){
					item.status = 1;
					that.msg = {
						status:1,
						title:`正在操作${item.title}，提现金额:${vk.pubfn.priceFilter(item.withdrawal_money)}元`
					};
					let res = await vk.callFunction({
						url: 'admin/business/sys.withdrawal.examine',
						needAlert:true,
						data:{
							_id: item._id,
							mode: that.form1.data.mode,
							seller_remark: that.form1.data.seller_remark,
							status: status,
							duanxin: process.env.NODE_ENV === "production"
						},
						fail:function(res){
							item.status = 0;
							that.msg = {
								status:3,
								title:res.msg
							};
							vk.hideLoading();
							that.page.loading = false;
						},
					});
					if(res.code === 0){
						item.status = status;
						that.msg = {
							status:3,
							title:"操作成功"
						};
					}
				}else{
					sleep = 0;
				}
				num++;
				that.progress.value = parseFloat((num/list.length*100).toFixed(2));
				if(sleep) await vk.pubfn.sleep(1000);
			}
			vk.hideLoading();
			that.page.loading = false;
			if(that.progress.value>=100){
				that.onFormSuccess();
			}
		}
	},
	watch: {
	
	},
	// 计算属性
	computed: {}
};
</script>

<style lang="scss" scoped></style>
